package com.ncs.leisure.beans.ui;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.ncs.leisure.beans.memory.BeansMember;
import com.ncs.leisure.beans.memory.MapList;
import com.ncs.leisure.beans.memory.MapMirror;
import com.ncs.leisure.beans.ui.vo.BeansMemberVo;
import com.ncs.leisure.beans.ui.vo.Map;
import com.ncs.leisure.beans.ui.vo.MapMenu;
import com.ncs.leisure.overall.ui.BasicForm;
import com.ncs.leisure.overall.ui.vo.MemberBean;

public class SpaceForm extends BasicForm {

	//接受用户指令
	
	public String acceptCtrlList(String ctrlChar){
		
		if(ctrlChar==null || ctrlChar.equals("")){
			
			return "ok";
			
		}
		
		// 上 & 下 ( 左 % 右 '
			
		Integer roomId= (Integer)session.getAttribute("BeansRoomId");
		
		MapList mapList=(MapList)ctx.getServletContext().getAttribute("BeansHall");
		
		MapMirror mm=mapList.findMapMirrorByIdObject(roomId);
		
		MemberBean memberBean=(MemberBean)session.getAttribute("baseMember");
		
		if(mm==null){
			
			return "error:not login";
			
		}
		
		return mm.movePersonXY(memberBean.getId(), ctrlChar);
		
	}
	
	//刷新个人信息
	
	public String freshMe(){
		
		this.loadSpring();
		
		MemberBean mb=(MemberBean)session.getAttribute("baseMember");
		
		BeansMemberVo bmv=(BeansMemberVo)beansContactBusiness.findBeansMemberVo(mb.getId());
		
		JSONObject json=JSONObject.fromObject(bmv);
		
		return json.toString();
		
	}

	
	//刷新空间
	
	public String freshRoom(){
		
		this.loadSpring();
		
		Integer roomId= (Integer)session.getAttribute("BeansRoomId");
		
		MapList mapList=(MapList)ctx.getServletContext().getAttribute("BeansHall");
		
		return mapList.findMapMirrorByIdString(roomId);
		
	}
	
	//变更地图
	
	public String changeMap(Integer mapId){
		
		Integer roomId= (Integer)session.getAttribute("BeansRoomId");
		
		MapList mapList=(MapList)ctx.getServletContext().getAttribute("BeansHall");
		
		MapMirror mm=mapList.findMapMirrorByIdObject(roomId);
		
		MemberBean memberBean=(MemberBean)session.getAttribute("baseMember");
		
		if(mm==null){
			
			return "error:not login";
			
		}
		
		//用户权限不正确
		
		if(mm.getOwner().compareTo(memberBean.getId())!=0){
			
			return "error:not owner";
			
		}
		
		//查找并获得正确的对象赋值
		
		Map map=(Map)beansContactBusiness.findMapMirrorById(mapId);
		
		mm.setMap(map);
		
		//JSONObject json=JSONObject.fromObject(map);
		
		return "ok";
	}
	
	//获得地图列表
	
	public String getAllMap(){
		
		this.loadSpring();
		
		List<Map> mapList= (List<Map>) beansContactBusiness.findMapList();
		
		List mapMenus=new ArrayList();
		
		MapMenu mapMenu=null;
		
		for(int i=0;i<mapList.size();i++){
			
			mapMenu=new MapMenu();
			
			mapMenu.setId(mapList.get(i).getId());
			
			mapMenu.setName(mapList.get(i).getName());
			
			mapMenu.setImg(mapList.get(i).getImg());
			
			mapMenus.add(mapMenu);
			
		}
		
		JSONArray jsonArray=JSONArray.fromObject(mapMenus);
		
		return jsonArray.toString();
		
	}
	
	//设置自身在房间的状态
	
	public String setMyState(String st){
		
		this.loadSpring();
		
		Integer roomId= (Integer)session.getAttribute("BeansRoomId");
		
		MemberBean mb=(MemberBean)session.getAttribute("baseMember");

		MapList mapList=(MapList)ctx.getServletContext().getAttribute("BeansHall");
		
		MapMirror mm=mapList.findMapMirrorByIdObject(roomId);
		
		BeansMember bm=mm.getPersonList().get(mb.getId());
		
		//非房主
		
		if(mb.getId().compareTo(mm.getOwner())!=0){
			
			//设置准备
			
			if(st.equals("ready")){
				
				bm.setState("ready");
				
				return "ok";
				
			//取消准备
				
			}else if(st.equals("wait")){
				
				bm.setState("wait");
				
				return "ok";
				
			}	
			
			return "error:not done";
					
		//房主
		
		}else{

			//判断是否有没有准备的玩家
			
			if(mm.isAllReady()){
				
				String mapb=mm.getMap().getMapBody();
				
				String mapbf="";
				
				//临时添加(计算出生点地图)
				
				Iterator iterator=mm.getPersonList().keySet().iterator();
				
				int n=1;
				
				for(int i=0;i<mapb.length();i++){
					
					if(mapb.substring(i, i+1).equals("0")){
						
						if(iterator.hasNext()){
							
							mm.getPersonList().get(iterator.next()).setState(new Integer(n).toString());
							
							mapbf=mapbf+n;
							
							n++;
							
						}else{
							
							mapbf=mapbf+"_";
							
						}
						
					}else{
						
						mapbf=mapbf+mapb.substring(i, i+1);
						
					}

					
				}
				
				mm.getMap().setMapBody(mapbf);
				
				mm.getMap().setMapBodyCopy(mapb.replace("0", "_"));
				
				//临时添加
				
				mm.setState("gameing");
				
				return "ok";
				
			}else{
				
				return "error:not all";
				
			}
		}
		
		
	}
	
	
}
